import React from 'react'
import {
	NavigationActions,
	NavigationContainerComponent,
	NavigationParams,
	StackActions,
} from 'react-navigation'

export const navigator = React.createRef<NavigationContainerComponent>()

export function navigate(routeName: string, params?: NavigationParams) {
	if (navigator) {
		const action = NavigationActions.navigate({ routeName, params })
		navigator.current && navigator.current.dispatch(action)
	}
}

export function navigatePush(routeName: string, params?: NavigationParams) {
	if (navigator) {
		const pushAction = StackActions.push({ routeName, params })
		navigator.current && navigator.current.dispatch(pushAction)
	}
}

export function navigateReplace(routeName: string, params?: NavigationParams) {
	if (navigator) {
		const replaceAction = StackActions.replace({ routeName, params })
		navigator.current && navigator.current.dispatch(replaceAction)
	}
}

export function goBack() {
	if (navigator) {
		const action = NavigationActions.back({})
		navigator.current && navigator.current.dispatch(action)
	}
}